//
//  XJComposeToolBar.swift
//  新浪微博
//
//  Created by 徐绩 on 16/4/26.
//  Copyright © 2016年 ayumi. All rights reserved.
//

import UIKit

//不同按钮的点击
enum XJComposeToolBarButtonType: Int {
    case Picture = 0, Mention, Trend, Emoticon, Add
}


class XJComposeToolBar: UIStackView {
    
    /// 判断是否是系统键盘
    var isSystemKeyboard: Bool = true {
        
        didSet{
            
            let btn = self.viewWithTag((XJComposeToolBarButtonType.Emoticon).rawValue) as? UIButton
            
            var name = "compose_keyboardbutton_background"
            
            if isSystemKeyboard {
            
            name = "compose_emoticonbutton_background"
            }
            btn?.setImage(UIImage(named: name), forState: .Normal)
            btn?.setImage(UIImage(named: "\(name)__highlighted"), forState: .Highlighted)
            
        }
    }
    
    //按钮点击执行闭包
    var buttonClickClosure: ((type: XJComposeToolBarButtonType)->())?

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
        
        distribution = .FillEqually
        
        addChildBtn("compose_toolbar_picture", type: .Picture)
        addChildBtn("compose_mentionbutton_background", type: .Mention)
        addChildBtn("compose_trendbutton_background", type: .Trend)
        addChildBtn("compose_emoticonbutton_background", type: .Emoticon)
        addChildBtn("compose_add_background", type: .Add)
        
        
    }
    
    private func addChildBtn(imageName: String,type: XJComposeToolBarButtonType){
    
        let btn = UIButton()
        //取到枚举的原始值
        btn.tag = type.rawValue
        
        btn.addTarget(self, action: "didClickToolBarBtn:", forControlEvents: .TouchUpInside)
        
        btn.setImage(UIImage(named: imageName), forState: .Normal)
        btn.setImage(UIImage(named: "\(imageName)_highlighted"), forState: .Highlighted)
        btn.setBackgroundImage(UIImage(named: "compose_toolbar_background"), forState: .Normal)
        btn.setBackgroundImage(UIImage(named: "compose_toolbar_background"), forState: .Highlighted)
        
        addArrangedSubview(btn)

    }
    /**
     toolBar按钮点击事件
     */
    @objc private func didClickToolBarBtn (button: UIButton){
    
        buttonClickClosure?(type: XJComposeToolBarButtonType(rawValue: button.tag)!)
    }

}
